from django.shortcuts import render
from rest_framework.generics import ListAPIView
from .serializers import SKUSerializer, SKUSearchSerializer
from goods.utils import StandardResultsSetPagination
from rest_framework.filters import OrderingFilter
from goods.models import SKU
from drf_haystack.viewsets import HaystackViewSet

# Create your views here.

"""
请求方式： GET /categories/(?P<category_id>\d+)/skus?page=xxx&page_size=xxx&ordering=xxx
categroy_id	    int	是	类别id（第三级类别）
page	        int	是	页数
page_size	    int	否	每页数量
ordering	    str	是	排序关键字（'create_time', 'price', 'sales'）
"""


class SKUListView(ListAPIView):
    serializer_class = SKUSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [OrderingFilter]
    ordering_fields = ['price', 'create_time', 'sales']

    def get_queryset(self):
        pk = self.kwargs['pk']
        return SKU.objects.filter(category_id=pk, is_launched=True)


class SKUSearchView(HaystackViewSet):
    index_models = [SKU]

    serializer_class = SKUSearchSerializer
